using AdminApiTemplate.Common.Helpers;
using System.ComponentModel;

namespace AdminApiTemplate.Cache
{
    public class ConfigHelper
    {
        private static IEnumerable<SysConfigItem> SysConfigs { get; set; }

        public static void Init(List<SysConfigItem> configs)
        {
            SysConfigs = configs;
        }

        public static T GetSysConfig<T>(string key, T defaultValue = default(T))
        {
            if (SysConfigs!=null)
            {
                var config = SysConfigs.Where(w => w.KeyName.ToLower()==key.ToLower()).FirstOrDefault();
                if (config!=null)
                {
                    return ConvertHelper.GetVal<T>(config.Val);
                }
            }
            return defaultValue;
        }
    }

    public class SysConfigItem
    {
        /// <summary>
        /// 键
        /// </summary>
        [Description("键")]
        public string KeyName { get; set; }

        /// <summary>
        /// 值
        /// </summary>
        [Description("值")]
        public string Val { get; set; }
    }
}
